#include "QTimeView.h"
#include <QHeaderView>
#include <QMouseEvent>
#include "QTimeModel.h"

QTimeView::QTimeView(QWidget *parent)
    : QTableView(parent)
{
    setTabKeyNavigation(false);
    setShowGrid(false);
    setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);

    verticalHeader()->setVisible(false);
    horizontalHeader()->setVisible(false);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    setSelectionBehavior(QAbstractItemView::SelectItems);
    setSelectionMode(QAbstractItemView::SingleSelection);
    horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    horizontalHeader()->setSectionsClickable(false);
    verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    verticalHeader()->setSectionsClickable(false);

    setFrameStyle(QFrame::NoFrame);
}

void QTimeView::internalUpdate()
{
    updateGeometries();
}

void QTimeView::mousePressEvent(QMouseEvent *event)
{
    auto pModel = qobject_cast<QTimeModel *>(model());
    if (!pModel) {
        QTableView::mousePressEvent(event);
        return;
    }
    if (event->button() != Qt::LeftButton)
        return;

    int value = handleMouseEvent(event);
    if (value != -1) {
        _validValueClicked = true;
        int row = -1, col = -1;
        pModel->cellForValue(value, &row, &col);
        if (row != -1 && col != -1) {
            selectionModel()->setCurrentIndex(pModel->index(row, col), QItemSelectionModel::NoUpdate);
        }
    }
    else {
        _validValueClicked = false;
        event->ignore();
    }
}

void QTimeView::mouseMoveEvent(QMouseEvent *event)
{
    auto pModel = qobject_cast<QTimeModel *>(model());
    if (!pModel) {
        QTableView::mouseMoveEvent(event);
        return;
    }
    if (_validValueClicked) {
        int value = handleMouseEvent(event);
        if (value != -1) {
            int row = -1, col = -1;
            pModel->cellForValue(value, &row, &col);
            if (row != -1 && col != -1) {
                selectionModel()->setCurrentIndex(pModel->index(row, col), QItemSelectionModel::NoUpdate);
            }
        }
    }
    else {
        event->ignore();
    }
}

void QTimeView::mouseReleaseEvent(QMouseEvent *event)
{
    auto pModel = qobject_cast<QTimeModel *>(model());
    if (!pModel) {
        QTableView::mouseReleaseEvent(event);
        return;
    }
    if (event->button() != Qt::LeftButton)
        return;
    if (_validValueClicked) {
        int value = handleMouseEvent(event);
        if (value != -1) {
            int row = -1, col = -1;
            pModel->cellForValue(value, &row, &col);
            if (row != -1 && col != -1) {
                selectionModel()->setCurrentIndex(pModel->index(row, col), QItemSelectionModel::SelectCurrent);
            }
            emit valueChanged(value);
        }
        _validValueClicked = false;
    }
    else {
        event->ignore();
    }
}

int QTimeView::handleMouseEvent(QMouseEvent *event)
{
    auto pModel = qobject_cast<QTimeModel *>(model());
    if (pModel) {
        const QPoint& pos = event->pos();
        const QModelIndex& index = indexAt(pos);
        int value = pModel->valueForCell(index.row(), index.column());
        if (value >= pModel->minValue() && value <= pModel->maxValue()) {
            return value;
        }
    }
    return -1;
}
